package com.ruoyi.web.controller.cashier;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.MemberList;
import com.ruoyi.system.domain.MemberStoredValueCard;
import com.ruoyi.system.domain.vo.MemberStoredValueCardVO;
import com.ruoyi.system.dto.MemberStoredValueCardDTO;
import com.ruoyi.system.dto.TransferAccountsDTO;
import com.ruoyi.system.dto.TransferDTO;
import com.ruoyi.system.service.IMemberListService;
import com.ruoyi.system.service.IMemberStoredValueCardService;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/memberStoredValueCard")
@Api(tags = "会员储值卡管理")
public class IMemberStoredValueCardController extends BaseController {

    @Autowired
    private IMemberStoredValueCardService memberStoredValueCardService;

    @Autowired
    private IMemberListService memberListService;

    @RequestMapping("/list")
    @ApiOperation("会员储值卡列表")
    public BaseResponse<IPage<MemberStoredValueCard>> list(@RequestBody MemberStoredValueCard memberStoredValueCard) {
        IPage<MemberStoredValueCard> listIPage = memberStoredValueCardService.byPage(memberStoredValueCard);
        return BaseResponse.success(listIPage);
    }

    /**
     * 购买定额卡
     */
    @PostMapping("/save")
    @ApiOperation("购买定额卡")
    public BaseResponse<Boolean> save(@RequestBody MemberStoredValueCard memberStoredValueCard) {
        boolean save = memberStoredValueCardService.add(memberStoredValueCard);
        return BaseResponse.success(save);
    }

    /**
     * 办理充值卡
     */
    @PostMapping("/recharge")
    @ApiOperation("办理充值卡")
    public BaseResponse<Boolean> recharge(@RequestBody MemberStoredValueCard memberStoredValueCard) {
        boolean recharge = memberStoredValueCardService.recharge(memberStoredValueCard);
        return BaseResponse.success(recharge);
    }

    /**
     * 充值卡-充值
     */
    @PostMapping("/rechargeCard")
    @ApiOperation("充值卡-充值")
    public BaseResponse<Boolean> rechargeCard(@RequestBody MemberStoredValueCardDTO cardDTO) {
        boolean recharge = memberStoredValueCardService.rechargeCard(cardDTO);
        return BaseResponse.success(recharge);
    }

    /**
     * 停用
     */
    @GetMapping("/disable")
    @ApiOperation("停用")
    public BaseResponse<Boolean> disable(Long id, String cardNumber) {
        if (AbdulEmptyUtils.isEmpty(id)) {
            return BaseResponse.error("请选择要停用的会员储值卡");
        }
        if (AbdulEmptyUtils.isEmpty(cardNumber)) {
            return BaseResponse.error("请输入会员储值卡编号");
        }
        LambdaQueryWrapper<MemberStoredValueCard> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MemberStoredValueCard::getId, id);
        wrapper.eq(MemberStoredValueCard::getCardNumber, cardNumber);
        MemberStoredValueCard memberStoredValueCard = memberStoredValueCardService.getOne(wrapper);
        if (AbdulEmptyUtils.isEmpty(memberStoredValueCard)) {
            return BaseResponse.error("会员储值卡不存在");
        }
        memberStoredValueCard.setStatus(2);
        boolean updated = memberStoredValueCardService.updateById(memberStoredValueCard);
        return BaseResponse.success(updated);
    }

    /**
     * 赠送余额
     */
    @PostMapping("/give")
    @ApiOperation("赠送余额")
    public BaseResponse<Boolean> give(@RequestBody MemberStoredValueCardDTO cardDTO) {
        return BaseResponse.success(memberStoredValueCardService.give(cardDTO));
    }

    /**
     * 余额退款
     */
    @PostMapping("/refund")
    @ApiOperation("余额退款")
    public BaseResponse<Boolean> refund(@RequestBody MemberStoredValueCardDTO cardDTO) {
        return BaseResponse.success(memberStoredValueCardService.refund(cardDTO));
    }

    /**
     * 转卡
     */
    @PostMapping("/transfer")
    @ApiOperation("转卡")
    public BaseResponse<Boolean> transfer(@RequestBody TransferDTO transferDTO) {
        Boolean transfer = memberStoredValueCardService.transfer(transferDTO);
        return BaseResponse.success(transfer);
    }

    /**
     * 转账
     */
    @PostMapping("/transfer_accounts")
    @ApiOperation("转账")
    public BaseResponse<Boolean> transferAccounts(@RequestBody TransferAccountsDTO transferAccountsDTO) {
        Boolean transfer = memberStoredValueCardService.transferAccounts(transferAccountsDTO);
        return BaseResponse.success(transfer);
    }

    /**
     * 积分兑换余额
     */
    @PostMapping("/integral_exchange")
    @ApiOperation("积分兑换余额")
    public BaseResponse<Boolean> integralExchange(@RequestBody MemberStoredValueCardDTO cardDTO) {
        //todo 积分兑换余额 需完善

        boolean exchange = memberStoredValueCardService.integralExchange(cardDTO);
        return BaseResponse.success(exchange);
    }

    /**
     * 会员储值卡详情
     */
    @GetMapping("/detail")
    @ApiOperation("会员储值卡详情")
    public BaseResponse<MemberStoredValueCardVO> detail(String cardNumber) {
        if (AbdulEmptyUtils.isEmpty(cardNumber)) {
            return BaseResponse.error("请选择会员储值卡");
        }
        LambdaQueryWrapper<MemberStoredValueCard> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MemberStoredValueCard::getCardNumber, cardNumber);
        MemberStoredValueCard memberStoredValueCard = memberStoredValueCardService.getOne(wrapper);
        if (AbdulEmptyUtils.isEmpty(memberStoredValueCard)) {
            return BaseResponse.error("会员储值卡不存在");
        }
        MemberStoredValueCardVO memberStoredValueCardVO = new MemberStoredValueCardVO();
        BeanUtils.copyProperties(memberStoredValueCard, memberStoredValueCardVO);
        MemberList memberListServiceById = memberListService.getById(memberStoredValueCard.getMemberId());
        if (AbdulEmptyUtils.isEmpty(memberListServiceById)) {
            throw new RuntimeException("会员不存在");
        }
        memberStoredValueCardVO.setMemberName(memberListServiceById.getMemberName());
        memberStoredValueCardVO.setMemberCard(memberListServiceById.getMemberCardNo());
        return BaseResponse.success(memberStoredValueCardVO);
    }

    /**
     * 修改别称
     */
    @GetMapping("/update_alias")
    @ApiOperation("修改别称")
    public BaseResponse<Boolean> updateAlias(String cardNumber, String cardAlias) {
        if (AbdulEmptyUtils.isEmpty(cardNumber)) {
            return BaseResponse.error("请选择会员储值卡");
        }
        if (AbdulEmptyUtils.isEmpty(cardAlias)) {
            return BaseResponse.error("请输入会员储值卡别称");
        }
        LambdaQueryWrapper<MemberStoredValueCard> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MemberStoredValueCard::getCardNumber, cardNumber);
        MemberStoredValueCard memberStoredValueCard = memberStoredValueCardService.getOne(wrapper);
        if (AbdulEmptyUtils.isEmpty(memberStoredValueCard)) {
            return BaseResponse.error("会员储值卡不存在");
        }
        memberStoredValueCard.setCardAlias(cardAlias);
        return BaseResponse.success(memberStoredValueCardService.updateById(memberStoredValueCard));
    }
}
